#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os, os.path, sys

import time
import tempfile
import unittest

from foxglove import log
from foxglove.cache import Cache

DASH = '-'

def makeData(key) :
    return DASH.join([key] * 4)
    # makeData()


class   TestCache(unittest.TestCase) :
    def setUp(self) :
        self.store_dir = tempfile.mkdtemp(suffix='.dir', prefix='cache_')
        log.debug('store_dir=%s', self.store_dir)
        # setUp()

    def removeDirs(self, dir_name) :
        items = os.listdir(dir_name)
        for item in items :
            dir_item = '%s/%s' % (dir_name, item)
            if os.path.isdir(dir_item) :
                self.removeDirs(dir_item)
            else :
                #log.debug('CONTENT of file %s', dir_item)
                #for line in file(dir_item) :
                #    log.debug('%s', line.strip())
                log.debug('remove file %s', dir_item)
                os.remove(dir_item)
        if os.path.exists(dir_name) :
            log.debug('remove directory %s', dir_name)
            os.removedirs(dir_name)
        # removedirs()

    def tearDown(self) :
        if os.path.exists(self.store_dir) :
            self.removeDirs(self.store_dir)
        # tearDown()

    def test_001(self) :
        tablet = Cache(self.store_dir)

        num_records = 1024

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # insert keys in reverse order
            data = makeData(key)
            status = tablet.insert(key, data)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status = tablet.insert(key, data)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.SUCCESS)

        del tablet
        # test_001()

    def test_002(self) :
        tablet = Cache(self.store_dir)

        num_records = 1024
        timeout = 1

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # insert keys in reverse order
            data = makeData(key)
            status = tablet.insert(key, data, timeout)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status = tablet.insert(key, data, timeout)
            self.assertEqual(status, Cache.SUCCESS)

        time.sleep(2)

        # here, should EXPIRED
        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.EXPIRED)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.EXPIRED)

        # here, should NOT found
        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            ## value is None when NOT FOUND
            #self.assertEqual(data, value)
            self.assertEqual(status, Cache.NOTFOUND)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            ## value is None when NOT FOUND
            #self.assertEqual(data, value)
            self.assertEqual(status, Cache.NOTFOUND)

        del tablet
        # test_002()

    def test_003(self) :
        tablet = Cache(self.store_dir, 4)

        num_records = 1024

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # insert keys in reverse order
            data = makeData(key)
            status = tablet.insert(key, data)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status = tablet.insert(key, data)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.SUCCESS)

        del tablet
        # test_003()

    def test_004(self) :
        tablet = Cache(self.store_dir, 4)

        num_records = 1024
        timeout = 1

        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # insert keys in reverse order
            data = makeData(key)
            status = tablet.insert(key, data, timeout)
            self.assertEqual(status, Cache.SUCCESS)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status = tablet.insert(key, data, timeout)
            self.assertEqual(status, Cache.SUCCESS)

        time.sleep(2)

        # here, should EXPIRED
        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.EXPIRED)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            self.assertEqual(data, value)
            self.assertEqual(status, Cache.EXPIRED)

        # here, should NOT found
        for x in range(num_records/2):
            key = '%032d' % (num_records - x)  # find keys in reverse order
            data = makeData(key)
            status,value = tablet.lookup(key)
            ## value is None when NOT FOUND
            #self.assertEqual(data, value)
            self.assertEqual(status, Cache.NOTFOUND)

        for x in range(num_records/2-1):
            key = '%032d' % x  # and now some in forward order
            data = makeData(key)
            status,value = tablet.lookup(key)
            ## value is None when NOT FOUND
            #self.assertEqual(data, value)
            self.assertEqual(status, Cache.NOTFOUND)

        del tablet
        # test_004()

    # class TestCache

#######################################################################

def test_suite():
    suite = unittest.TestSuite()

    suite.addTest(unittest.makeSuite(TestCache))

    return suite

if  __name__ == '__main__' :
    #log.setLogPath('/dev/null')
    log.enableDebug()

    unittest.main()
